/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package MainPack;

import EntityGameObject.Pet;
import Supporter.GlobalVariables;
import java.io.IOException;
import java.io.InputStream;
import java.util.Random;
import javax.microedition.media.MediaException;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.media.Manager;
import javax.microedition.media.Player;
import javax.microedition.rms.RecordStoreException;

/**
 * @author Van persie
 */

public class MainMIDlet extends MIDlet {
     public static final int SCENE_MENU = 0;
     public static final int SCENE_MANAGEMENT = 1;
     public static final int SCENE_PLAYPET = 2;
     public static final int SCENE_ACTIONTIME = 3;
     public static final int SCENE_SETNAME = 4;
     public static final int SCENE_PLAYGAME = 5;
     public static final int SCENE_RESULT = 6;
     public static final int SCENE_SAVE = 7;
    
	private Display display; // The display

	//private ImageCanvas canvas; // Canvas
        private MenuCanvas MenuCanvas;
        private PetManagement PetManagementCanvas;
        private PlayPetCanvas PlayPetCanvas;
        private SetNameCanvas SetNameCanVas;
        private GameCanvasView gameCanvas;
        private ResultCanvas ResultCanvas;
        private SaveCanvas SaveCanvas;
        private Pet pet = null;
        private InputStream in;
        private Player myPlayer;
        int index = -1;
        
	public MainMIDlet() throws IOException, MediaException {
            System.out.print("beforepet");
                pet = new Pet();
                
              System.out.print("beforepet");  
                display = Display.getDisplay(this);
                GlobalVariables.myRandom = new Random();
                //this.gameCanvas = new GameCanvasView();
                //this.gameCanvas.createCanvas(this);

	}


    public void toPetManamagementScene() {
        PetManagementCanvas = new PetManagement(this);  
        PetManagementCanvas.setFullScreenMode(true);
        display.setCurrent(PetManagementCanvas);
    }

    public void toPlayGameScene() throws IOException, MediaException {
         in = getClass().getResourceAsStream("/launch.wav");
       
            myPlayer = Manager.createPlayer(in,"audio/X-wav");
            myPlayer.setLoopCount(1);
        gameCanvas = new GameCanvasView(myPlayer);
        gameCanvas.createCanvas(this);
        display.setCurrent(gameCanvas);
        return;
    }

    public void toPlayPetScene() {
        PlayPetCanvas = new PlayPetCanvas(this);
        PlayPetCanvas.setFullScreenMode(true);
        display.setCurrent(PlayPetCanvas);
        System.out.println("Play Pet");
    }

    public void toResultScene() throws IOException {
        ResultCanvas = new ResultCanvas();
        ResultCanvas.createCanvas(this);
        ResultCanvas.setFullScreenMode(true);
        display.setCurrent(ResultCanvas);
    }

    public void toSetNameScene() throws IOException, MediaException {
        in = getClass().getResourceAsStream("/buttonclick.wav");
       myPlayer.deallocate();
            myPlayer = Manager.createPlayer(in,"audio/X-wav");
            myPlayer.setLoopCount(1);
        SetNameCanVas = new SetNameCanvas(this,myPlayer);
        SetNameCanVas.setFullScreenMode(true);
        display.setCurrent(SetNameCanVas);
    }
    public void toLoadScene() throws RecordStoreException
    {
        SaveCanvas = new SaveCanvas(this,false);
        SaveCanvas.setFullScreenMode(true);
        display.setCurrent(SaveCanvas);
    }
    public void toSaveScene() throws RecordStoreException{
        SaveCanvas = new SaveCanvas(this,true);
        SaveCanvas.setFullScreenMode(true);
        display.setCurrent(SaveCanvas);
    }
    protected void startApp() {   
//        index = SCENE_MENU;
//        toMenuScene();

   //     index = SCENE_PLAYGAME;
  //      toPlayGameScene();

        index = SCENE_MENU;
        try {
            toMenuScene();
     //        toPetManamagementScene();
     //        toPlayPetScene();
     //        toPlayPetScene();
        } catch (MediaException ex) {
            ex.printStackTrace();
        }
       

              
    }

    public void toMenuScene() throws MediaException {
        try {
            in = getClass().getResourceAsStream("/songs.mid");
       
            myPlayer = Manager.createPlayer(in,"audio/midi");
            myPlayer.setLoopCount(1);
            MenuCanvas = new MenuCanvas(this,myPlayer);
        } catch (IOException ex) {
            ex.printStackTrace();
           System.out.println(ex);
        }

        MenuCanvas.setFullScreenMode(true);
        display.setCurrent(MenuCanvas);
    }
        public void GarbageCollect()
        {
            switch(index)
            {
                case SCENE_MENU:                    
                    MenuCanvas = null;
                    break;
                    
                case SCENE_MANAGEMENT:  
                    PetManagementCanvas = null;
                    break;
                case SCENE_SETNAME:
                    SetNameCanVas= null;
                    break;
                case SCENE_PLAYGAME:                    
                    gameCanvas = null;
                    break;
                    
                case SCENE_RESULT:
                    
                   // ResultCanvas = null;
                    break;
                    
                case SCENE_PLAYPET:                    
                    PlayPetCanvas = null;
                    break;
                    
            }
        }
        public void SwitchTo(int canvas) throws IOException, MediaException
        {
            switch(canvas)
            {
                case SCENE_MENU:
                    toMenuScene();
                    break;
                    
                case SCENE_MANAGEMENT:  
                    toPetManamagementScene();
                    break;
                case SCENE_SETNAME:
                    toSetNameScene();
                    break;
                case SCENE_PLAYGAME:                   
                    toPlayGameScene();
                    break;
                    
                case SCENE_RESULT:
                    toResultScene();
                    break;
                    
                case SCENE_PLAYPET:                    
                    toPlayPetScene();
                    break;
            }
            GarbageCollect();
            System.gc();
            index = canvas;
        }
        

	protected void pauseApp() {
	}

        
	protected void destroyApp(boolean unconditional) {
            
	}

	public void exitMIDlet() {
		destroyApp(true);
		notifyDestroyed();
	}

    /**
     * @return the pet
     */
    public Pet getPet() {
        return pet;
    }

    /**
     * @param pet the pet to set
     */
    public void setPet(Pet pet) {
        this.pet = pet;
    }
}
